package com.plugins.utils;

import android.util.Log;

import java.io.BufferedReader;
import java.io.DataOutputStream;
import java.io.InputStreamReader;

/**
 * 常用工具
 */
public class Utils {
     /**
     * 获取总页数
     * @param total
     * @param pageSize
     * @return
     */
    public static int getPageCount(int total,int pageSize){
        return (total-1)/pageSize+1 ;
    }

    /**
     * 进度
     * @param current
     * @param allSize
     * @return
     */
    public static int getProgress(int current,int allSize){
        return (int)((float)current/allSize*100);
    }

    /**
     * 开启adb调试
     * @param
     * @return
     */
    public static boolean adbStart() {
        try {

            Utils.setProp("service.adb.tcp.port", "5555");
            if (Utils.isProcessRunning("adbd")) {
                Utils.runRootCommand("stop adbd");
            }
            Utils.runRootCommand("start adbd");
        } catch (Exception e) {
            return false;
        }
        return true;
    }

    /**
     * 是否有root权限
     * @return
     */
    public static boolean hasRootPermission() {
        Process process = null;
        DataOutputStream os = null;
        boolean rooted = true;
        try {
            process = Runtime.getRuntime().exec("su");
            os = new DataOutputStream(process.getOutputStream());
            os.writeBytes("exit\n");
            os.flush();
            process.waitFor();
            if (process.exitValue() != 0) {
                rooted = false;
            }
        } catch (Exception e) {
            rooted = false;
        } finally {
            if (os != null) {
                try {
                    os.close();
                    process.destroy();
                } catch (Exception e) {
                }
            }
        }
        return rooted;
    }

    /**
     * 设置配置
     * @param property
     * @param value
     * @return
     */
    public static boolean setProp(String property, String value) {
        return runRootCommand("setprop " + property + " " + value);
    }

    /**
     * 单独运行命令
     * @param command
     * @return
     */
    public static boolean runRootCommand(String command) {
        Process process = null;
        DataOutputStream os = null;
        try{
            process = Runtime.getRuntime().exec("su");
            os = new DataOutputStream(process.getOutputStream());
            os.writeBytes(command + "\n");
            os.writeBytes("exit\n");
            os.flush();
            process.waitFor();
        }catch (Exception e){
            e.printStackTrace();
            return false;
        }finally {
            try{
                if (os != null){
                    os.close();
                }
                process.destroy();
            }
            catch (Exception e){}
        }
        return true;
    }



    /**
     * 进程是否在运行
     * @param processName
     * @return
     * @throws Exception
     */
    public static boolean isProcessRunning(String processName) throws Exception {
        boolean running = false;
        Process process = null;
        process = Runtime.getRuntime().exec("ps");
        BufferedReader in = new BufferedReader(new InputStreamReader(process.getInputStream()));
        String line = null;
        while ((line = in.readLine()) != null) {
            if (line.contains(processName)) {
                running = true;
                break;
            }
        }
        in.close();
        process.waitFor();
        Log.d("isProcessRunning",processName+"->"+running);
        return running;
    }
}
